import 'package:flutter/material.dart';
import 'package:flutter_bsbdj/utils/constant.dart';
import 'package:flutter_bsbdj/utils/shared_preferences_util.dart';

import 'uploadheader.dart';

class UserInfo extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return InfoWidget();
  }

}

class InfoWidget extends State<UserInfo>{

  String userName="";
  String pic="";

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getUser();
    _getPic();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Container(
          padding: EdgeInsets.only(right: 60),
          alignment: Alignment.center,
          child: Text("用户信息"),
        ),
      ),
      body:Container(
        color: Colors.white,
        child: Column(
          children: <Widget>[
            Expanded(child: ListView(
              children: <Widget>[
                Container(
                  height: 220,
                  child: GestureDetector(
                    child: Stack(
                      children: <Widget>[
                        Container(
                          child: Column(
                            children: <Widget>[
                              Container(
                                height: 170,
                                decoration: BoxDecoration(
                                    image: DecorationImage(image: AssetImage(Constant.IMAGE_HEADER_BG),fit: BoxFit.fill)
                                ),
                              ),
                              Expanded(child: Container(
                                color: Colors.white,
                              )),
                            ],
                          ),
                        ),
                        Container(
                          height: 100,
                          margin: EdgeInsets.only(top: 120),
                          alignment: Alignment.center,
                          child: Stack(
                            alignment: Alignment.center,
                            children: <Widget>[
                              Container(
                                width: 100,
                                height: 100,
                                decoration: BoxDecoration(
                                    color: Colors.white,
                                    borderRadius: BorderRadius.all(Radius.circular(50))
                                ),
                              ),
                              Container(
                                width: 90,
                                height: 90,
                                decoration: BoxDecoration(
                                    image: DecorationImage(image: pic.isEmpty?AssetImage(Constant.IMAGE_USER_HANDER):
                                    NetworkImage(pic)),
                                    borderRadius: BorderRadius.all(Radius.circular(45))
                                ),
                              )
                            ],
                          ),
                        )
                      ],
                    ),
                    onTap: (){
                      //上传头像
                      Navigator.of(context).push(MaterialPageRoute(builder: (context)=>UpLoadHeader())).then((picn){
                        if(picn!=null){
                          setState(() {
                            pic=picn;
                          });
                        }
                      });
                    },
                  ),
                ),
                Container(
                  alignment: Alignment.center,
                  height: 20,
                  child: Text(userName),
                ),
              ],
            ),),
            Container(
              alignment: Alignment.center,
              child: MaterialButton(onPressed: (){
                SharedPrefrenceUtil.setStringValue(Constant.USER_NAME, "");
                SharedPrefrenceUtil.setStringValue(Constant.USER_ID, "");
                SharedPrefrenceUtil.setStringValue(Constant.USER_PIC, "");
                Navigator.pop(context,Constant.LOGIN_OUT);
              },
                child: Text("退出登录",style: TextStyle(color: Colors.white),),
                color: Colors.blueAccent,
                minWidth: 240,
              ),
            )
          ],
        )
      )
    );
  }

  void _getUser()async{
        await SharedPrefrenceUtil.getStringValue(Constant.USER_NAME, "null").then((name){
          if(name!="null"){
            setState(() {
              userName=name;
            });
          }
        });
  }

  void _getPic()async{
    await SharedPrefrenceUtil.getStringValue(Constant.USER_PIC, "null").then((value){
      if(value!="null"){
        setState(() {
          pic=value;
        });
      }
    });
  }

}